<?php


namespace App\Model;


use App\Model\Finance\ErrDec;
use App\Model\Finance\Util;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisPool;

class User
{

    //改变积分
    public static function score($db, $score, $user_id, $memo, $ext = [])
    {
        //$db = MysqlPool::defer();
        $user = $db->where('id', $user_id)
            ->getOne(DataTable::USER_TABLE, 'score');
        if ($user && $score != 0) {
            //判断
            if (array_key_exists('type', $ext)) {
                if ($ext['type'] == '邀请') {
                    if ($user['score'] < 100) {
                        $score = 100 - $user['score'];
                    }
                }
                if ($ext['type'] == '签到') {
                    $updateobj['signin_time'] = $ext['value'];
                }
            }

            $before = $user['score'];
            $after = $user['score'] + $score;
            if ($after < 0) {
                throw new \Exception('SCORE_ERR');
            }
            $updateobj['score'] = $after;

            //更新用户
            $db->where('id', $user_id)->update(DataTable::USER_TABLE, $updateobj);
            $insertobj = ['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo, 'createtime' => time(), 'date_str' => date('Ymd')];
            //保存记录
            $db->insert(DataTable::SCORE_LOG_TABLE, $insertobj);

            //重置一下
            self::setScore($user_id);
        }
    }

    public static function getScore($uid)
    {
        $redis = RedisPool::defer();
        $score = $redis->get('cz_score_' . $uid);
        if (!$score) {
            $score = self::setScore($uid);
        }
        return $score;
    }

    public static function setScore($uid)
    {
        $db = MysqlPool::defer();
        $score = $db->where('id', $uid)
            ->getValue(DataTable::USER_TABLE, 'score');
        $redis = RedisPool::defer();
        $redis->set('cz_score_' . $uid, $score);
        return $score;
    }

    public static function setAppointment($user_id)
    {
        $redis = RedisPool::defer();
        $key = 'appointment_1_' . $user_id;
        $op = $redis->get($key);
        if ($op) {
            return ErrDec::OPT_ERR;
        }
        $redis->set($key, 1, 5);

        if (Util::isAppointment($user_id)) {
            $db = MysqlPool::defer();
            $updata['msg_status'] = 1;
            $db->where('user_id', $user_id)
                ->update(DataTable::THIRD_TABLE, $updata);
            //更新缓存
            $redis->set(CacheTable::IS_APPOINTMENT . $user_id, 0, 2592000);
        }
        return ErrDec::SUCCESS;
    }


    public static function cleanfollow()
    {
        $db = MysqlPool::defer();
        $db->setValue(DataTable::THIRD_TABLE, 'is_follow', 0);
    }

    public static function setfollow($openidlist)
    {
        $db = MysqlPool::defer();
        $db->whereIn('openid', $openidlist)
            ->setValue(DataTable::THIRD_TABLE, 'is_follow', 1);


    }

    public static function isGuessOnce($uid)
    {
        $db = MysqlPool::defer();
        $res = $db->where('user_id', $uid)
            ->whereLike('memo', '参与竞猜涨跌:%')
            ->count(DataTable::SCORE_LOG_TABLE);
        if ($res > 0) {
            return true;
        } else {
            return false;
        }

    }


    public static function isFollow($uid)
    {
        $key = CacheTable::IS_FOLLOW . $uid;
        $redis = RedisPool::defer();
        $data = $redis->get($key);

        if ($data === false) {
            $f = 0;
            $db = MysqlPool::defer();
            $count = $db->where('id', $uid)
                ->where('is_follow', 1)
                ->count(DataTable::THIRD_TABLE);
            if ($count > 0) {
                $f = 1;
            }

            $data = $f;
            $redis->set($key, $data, 2592000);
        }
        if ($data == 1) {
            return true;
        } else {
            return false;
        }
    }

    public static function isDeny($uid)
    {
        $db = MysqlPool::defer();
        $count = $db->where('id', $uid)
            ->where('status', 'hidden')
            ->Count(DataTable::USER_TABLE);

        if ($count > 0) {
            return true;
        } else {
            return false;
        }

    }
}